$(document).ready(function() {
	var userString = localStorage.getItem("user");
	var user = JSON.parse(userString);
	if (user != null) {
		var loginDate = new Date(user.date);
		var nowDate = new Date();
		if (loginDate.setDate(loginDate.getDate() + 7) > nowDate) {
			if (user.roleid == 1) {
				location.href = "./officeuser.html";
			}
			if (user.roleid == 2) {
				location.href = "AdministratorMenu.html";
			}
		}
	}

	$(".main .loginButton").click(function() {
		var email = $(".main .email").val();
		var password = $(".main .password").val();
		$.ajax({
			type : "GET",
			url : "./login",
			data : "Email=" + email + "&Password=" + password,
			success : function(json) {
				var msg = JSON.parse(json);
				if (msg.flag != "success") {
					$(".main .alertInfo").text(msg.flag);
				} else {
					var is7day = $(".main  .is7day").is(":checked");
					if (is7day = true) {
						var date = new Date();
						var dateString = date.toString();
						msg.data.Date = dateString;
						localStorage.setItem("user", JSON.stringify(msg.Email));
					}
					if (msg.data.RoleId == 1) {
						location.href = "./UserList.html";
					}
					if (msg.data.RoleId == 2) {
						location.href = "AdministratorMenu.html";
					}
				}
			}
		});
	});
});